package com.teama4.shop.controller;

import com.teama4.shop.domain.CommodityOrders;
import com.teama4.shop.domain.Orders;
import com.teama4.shop.service.CommodityOrdersService;
import com.teama4.shop.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.UUID;


@Controller
@RequestMapping("/Order")
public class OrderController {
    @Autowired
    private OrdersService ordersService;

    @Autowired
    private CommodityOrdersService commodityOrdersService;

    @GetMapping("/findOrder")
    public String findUser(@RequestParam("id") int id, Model model) {
        Orders order = ordersService.selectByPrimaryKey(id);
        model.addAttribute("order", order);
        model.addAttribute("title", "修改订单");
        return "/admin/order-add";
    }

    @GetMapping("/goToAddPage")
    public String goToAddPage(Model model){
        Orders orders = new Orders();
        model.addAttribute("order", new Orders());
        model.addAttribute("title", "增加订单");
        return "/admin/order-add";
    }

    @PostMapping("/addOrder")
    public String addOrder(Orders orders) {
        ordersService.saveOrder(orders);
        return "redirect:/index/orderList";
    }


    @DeleteMapping("/deleteOrder")
    @ResponseBody
    public Boolean deleteUser(@RequestParam("id") int id) {
        int deleted = ordersService.deleteByPrimaryKey(id);
        return deleted > 0;
    }

    @GetMapping("/showMessageContent")
    public String showMessageContent(int id, Model model) {
        Orders order = ordersService.selectByPrimaryKey(id);
        model.addAttribute("order", order);
        List<CommodityOrders> list = commodityOrdersService.selectByOrderId(order.getId());
        model.addAttribute("list", list);
        return "/merchant/order-message-show";
    }

    @GetMapping("/update")
    public String update(int id) {
        ordersService.updateOrderStatus(id);
        return "redirect:/index/merchantOrderList";
    }
}
